package edu.whu.assignment5.service.impl;

import edu.whu.assignment5.dataobject.Product;
import edu.whu.assignment5.dataobject.Supplier;
import edu.whu.assignment5.repository.ProductRepository;
import edu.whu.assignment5.repository.SupplierRepository;
import edu.whu.assignment5.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierRepository supplierRepository;
    @Autowired
    private ProductRepository productRepository;

    @Override
    public Supplier findById(int sid) {
        return supplierRepository.findBySid(sid);
    }

    @Override
    public int addSupplier(String sname, int pid) {
        Product exist = productRepository.findByPid(pid);
        if(exist == null) {
            return -1;
        }
        Supplier supplier = new Supplier();
        supplier.setSname(sname);
        supplier.setProduct(exist);
        Supplier result = supplierRepository.save(supplier);
        return result == null ? 0 : 1;
    }

    @Override
    public int deleteSupplier(int sid) {
        return supplierRepository.deleteBySid(sid);
    }
}
